﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System.Collections.Generic;
using System.Diagnostics;

namespace WidgetLibrary
{
    public static class SoundManager
    {
        private static SoundEffectInstance _soundEffectInstance;
        private static Dictionary<string, SoundEffectInstance> _soundBank;

        static SoundManager()
        {
            _soundBank = new Dictionary<string, SoundEffectInstance>();
        }


        /// <summary>
        /// Play a music background
        /// </summary>
        /// <param name="source">path to source music item</param>
        public static void PlayMusicBackground(string source)
        {
            try
            {
                if (MediaPlayer.GameHasControl && MediaPlayer.State != MediaState.Playing)
                    MediaPlayer.Play(Song.FromUri("background", new Uri(source, UriKind.Relative)));
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error playing the music background: " + e.Message);
            }
        }


        /// <summary>
        /// Stop a music background
        /// </summary>
        public static void StopMusicBackground()
        {
            try
            {
                if (MediaPlayer.State == MediaState.Playing)
                    MediaPlayer.Stop();
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error stopping the music background: " + e.Message);
            }
        }


        /// <summary>
        /// Play a sound effect
        /// </summary>
        /// <param name="source">path to sound effect</param>
        public static void PlayAudio(string source)
        {
            if (MediaPlayer.GameHasControl)
            {
                LoadSound(source, "false");
                Play(source);
            }
        }


        /// <summary>
        /// Play a sound effect
        /// </summary>
        /// <param name="source">path to sound effect</param>
        /// <param name="loop">"true" if is looping, "false" otherwise</param>
        public static void PlayAudio(string source, string loop)
        {
            if (MediaPlayer.GameHasControl)
            {
                LoadSound(source, loop);
                Play(source);
            }
        }


        /// <summary>
        /// Stop all sound effects
        /// </summary>
        public static void StopAudio()
        {
            foreach (string item in _soundBank.Keys)
            {
                try
                {
                    _soundBank[item].Stop();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Error stoping sound: " + e.Message);
                }
            }
        }


        /// <summary>
        /// Stop a sound effect
        /// </summary>
        /// <param name="source">path to sound effect</param>
        public static void StopAudio(string source)
        {
            try
            {
                if (_soundBank.ContainsKey(source))
                    _soundBank[source].Stop();
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error stoping sound: " + e.Message);
            }
        }


        /// <summary>
        /// Pause-Resume an audio effect
        /// </summary>
        /// <param name="source">path to audio effect</param>
        public static void PauseResumeAudio(string source)
        {
            if (_soundBank.ContainsKey(source))
            {
                switch (_soundBank[source].State)
                {
                    case SoundState.Paused:
                        _soundBank[source].Resume();
                        break;
                    case SoundState.Playing:
                        _soundBank[source].Pause();
                        break;
                    case SoundState.Stopped:
                        _soundBank[source].Play();
                        break;
                    default:
                        break;
                }
            }
        }


        /// <summary>
        /// Pause-Resume all audio effects
        /// </summary>
        public static void PauseResumeAllAudio()
        {
            foreach (string item in _soundBank.Keys)
            {
                PauseResumeAudio(item);
            }
        }


        /// <summary>
        /// Add audio effect to SoundBank
        /// </summary>
        /// <param name="source">path of the audio effect</param>
        /// <param name="loop">"true" if is lopping. "false" otherwise</param>
        private static void LoadSound(string source, string loop)
        {
            bool isLooped = bool.Parse(loop);
            if (!_soundBank.ContainsKey(source))
            {
                var stream = TitleContainer.OpenStream(source);
                SoundEffect effect = SoundEffect.FromStream(stream);
                _soundEffectInstance = effect.CreateInstance();
                _soundEffectInstance.IsLooped = isLooped;
                _soundBank.Add(source, _soundEffectInstance);
            }
            else
            {
                _soundBank[source].IsLooped = isLooped;
            }


        }

        /// <summary>
        /// Play an audio effect from SoundBank
        /// </summary>
        /// <param name="source"></param>
        private static void Play(string source)
        {
            try
            {
                _soundBank[source].Play();
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error playing sound effect: " + e.Message);
            }
        }



    }
}
